﻿// -----------------------------------------------------------------------
//  <copyright file="ApmStoreWebRequest.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
//  </copyright>
//  <author>Mike Smacinih: mihailsm@microsoft.com</author>
// -----------------------------------------------------------------------

namespace WpInsights.Core
{
    using System;
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    using System.Text;

    /// <summary>
    ///     Extension methods for HttpWebRequest
    /// </summary>
    public class ApmStoreWebRequest
    {
        #region Constants

        private const string TenantKeyHeader = "Key";

        #endregion

        #region Fields

        private readonly Uri requestUrl;

        private readonly EnvironmentSettings environmentSettings;

        #endregion

        #region Constructors and Destructors

        public ApmStoreWebRequest(
            Uri requestUrl,
            EnvironmentSettings environmentSettings)
        {
            this.requestUrl = requestUrl;
            this.environmentSettings = environmentSettings;

            ServicePointManager.ServerCertificateValidationCallback += RemoteCertValidate;
        }

        private static bool RemoteCertValidate(
            object sender,
            X509Certificate cert,
            X509Chain chain,
            System.Net.Security.SslPolicyErrors error)
        {
            //This is our custom validator method. The methods signature is defined simply to satisfy the 
            //RemoteCertificateValidationCallback delegate. 
            //Our custom validator will ignore the parameters and simply return true to convince the WCF runtime 
            //that the self-signed certificate can be trusted. 
            return true;
        }


        #endregion

        #region Delegates

        internal delegate HttpWebResponse GetWebResponseDelegate();

        #endregion


        #region Public Methods and Operators


        #endregion

        #region Methods

        public void Send(string dataItems)
        {
            var webRequest = (HttpWebRequest)WebRequest.Create(this.requestUrl);
            webRequest.Method = "POST";
            webRequest.Headers.Add(TenantKeyHeader, this.environmentSettings.VsoAccountId);
            webRequest.Headers.Add("Agent", "WindowsPhone");
            webRequest.Headers.Add("AgentVersion", "1.0");
            webRequest.Headers.Add("SourceType", "Azure");
            
            if (!string.IsNullOrEmpty(this.environmentSettings.VsoAccountId))
            {
                string body = dataItems;
                webRequest.Sign(this.environmentSettings.VsoAccountId, this.environmentSettings.PrimaryKey, body);
            }

            byte[] byteArray = Encoding.UTF8.GetBytes(dataItems);
            var requestStream = webRequest.GetRequestStream();
            requestStream.Write(byteArray, 0, dataItems.Length);
            webRequest.GetResponse();
        }


        #endregion
    }
}